package core.ws;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.scribe.model.Token;

/**
 *
 * @author Admin
 */
public class UserSession {

    private final String TOKENS = "session_tokens";
    private final WebContext webContext;

    public UserSession(HttpServletRequest request) {
        webContext = new WebContext(request);
    }

    public Collection<Storage> getStorages() {
        StorageList list = new StorageList();
        Map<String, Token> tokens = getTokens();
        for (String key : tokens.keySet()) {
            list.setAuthenticated(key, true);
        }
        return list.getList();
    }

    public Map<String, Token> getTokens() {
        if (!webContext.contains(TOKENS)) {
            webContext.set(TOKENS, new HashMap<String, Token>());
        }
        return (Map<String, Token>) webContext.get(TOKENS);
    }

    public boolean hasToken(String storageType) {
        return getTokens().containsKey(storageType);
    }

    public Token getToken(String storageType) {
        return getTokens().get(storageType);
    }

    public void setToken(String storageType, Token token) {
        getTokens().put(storageType, token);
    }

}
